<template>
  <div class="register-container">
    <h2>用户注册</h2>
    <form @submit.prevent="handleRegister" class="register-form">
      <div class="form-group">
        <label for="username">用户名</label>
        <input
          type="text"
          id="username"
          v-model="formData.username"
          placeholder="请输入用户名（3-20个字符）"
          required
        />
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input
          type="password"
          id="password"
          v-model="formData.password"
          placeholder="请输入密码（6-20个字符）"
          required
        />
      </div>
      <div class="form-group">
        <label for="confirm_password">确认密码</label>
        <input
          type="password"
          id="confirm_password"
          v-model="formData.confirm_password"
          placeholder="请再次输入密码"
          required
        />
      </div>
      <button type="submit" :disabled="isSubmitting">
        {{ isSubmitting ? '注册中...' : '注册' }}
      </button>
    </form>
    <div v-if="message" :class="['message', messageType]">
      {{ message }}
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import request from '../utils/request'
import { API_ENDPOINTS } from '../config/api'
import { useRouter } from 'vue-router'

const router = useRouter()
const formData = ref({
  username: '',
  password: '',
  confirm_password: ''
})

const isSubmitting = ref(false)
const message = ref('')
const messageType = ref('')

const handleRegister = async () => {
  // 重置消息
  message.value = ''
  messageType.value = ''

  // 基本验证
  if (formData.value.password !== formData.value.confirm_password) {
    message.value = '两次输入的密码不一致'
    messageType.value = 'error'
    return
  }

  isSubmitting.value = true
  try {
    const response = await request.post(API_ENDPOINTS.register, formData.value)
    message.value = response.data.message
    messageType.value = 'success'
    // 清空表单
    formData.value = {
      username: '',
      password: '',
      confirm_password: ''
    }
    // 注册成功后延迟跳转到登录页
    setTimeout(() => {
      router.push('/login')
    }, 1500)
  } catch (error) {
    message.value = error.response?.data?.error || '注册失败，请稍后重试'
    messageType.value = 'error'
  } finally {
    isSubmitting.value = false
  }
}
</script>

<style scoped>
.register-container {
  max-width: 400px;
  margin: 40px auto;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.register-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

label {
  font-weight: bold;
  color: #333;
}

input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

input:focus {
  outline: none;
  border-color: #4CAF50;
}

button {
  padding: 10px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
}

button:hover:not(:disabled) {
  background-color: #45a049;
}

button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.message {
  margin-top: 20px;
  padding: 10px;
  border-radius: 4px;
  text-align: center;
}

.success {
  background-color: #dff0d8;
  color: #3c763d;
  border: 1px solid #d6e9c6;
}

.error {
  background-color: #f2dede;
  color: #a94442;
  border: 1px solid #ebccd1;
}
</style> 